<template>
	<view class="view-contain">
		<public-module leftIcon="arrow-left" :title="title" bgColor="#4b80af" />
		
		<!-- 训练区域 -->
		<view class="training-area" v-if="trainingStarted">
			<!-- 试卷头部信息 -->
			<view class="paper-header">
				<view class="paper-info">
					<view class="info-item">
						<text class="info-label">题目数量：</text>
						<text class="info-value">10题</text>
					</view>
					<view class="info-item">
						<text class="info-label">答题时间：</text>
						<text class="info-value">{{ formatTime(remainingTime) }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">当前进度：</text>
						<text class="info-value">{{ currentQuestionIndex + 1 }}/10</text>
					</view>
				</view>
			</view>

			<!-- 材料内容区域 -->
			<view class="material-section">
				<scroll-view class="material-content" scroll-y>
					<rich-text :nodes="currentMaterial.content"></rich-text>
				</scroll-view>
			</view>

			<!-- 问题选项区域 -->
			<view class="options-section">
				<view class="question-content">
					<view class="question-title">
						<text class="question-number">{{ currentQuestionIndex + 1 }}.</text>
						<text class="question-text">{{ currentQuestion.question }}</text>
					</view>
				</view>
				<view class="options">
					<view 
						v-for="(option, index) in sortedOptions" 
						:key="option.label" 
						class="option" 
						:class="{ 
							'selected': selectedOption === option,
							'correct': showFeedback && option.isCorrect,
							'wrong': showFeedback && !option.isCorrect && selectedOption === option
						}"
						@click="selectOption(option)"
					>
						<text class="option-label">{{ option.label }}.</text>
						<text class="option-text">{{ option.text }}</text>
					</view>
				</view>
			</view>

			<!-- 答案反馈 -->
			<view class="feedback-area" v-if="showFeedback">
				<view :class="['feedback-text', isCorrect ? 'correct' : 'wrong']">
					{{ isCorrect ? '✓ 回答正确！' : '✗ 回答错误！' }}
				</view>
				<view class="correct-answer" v-if="!isCorrect">
					正确答案：{{ correctAnswerText }}
				</view>
				<view class="next-btn" @click="goToNextQuestion">
					{{ currentQuestionIndex < 9 ? '下一题' : '完成答题' }}
				</view>
			</view>
		</view>

		<!-- 完成页面 -->
		<view class="result-area" v-if="showResult">
			<view class="result-header">
				<view class="result-title">答题完成</view>
				<view class="result-subtitle">恭喜您完成本次找数训练！</view>
			</view>
			
			<view class="result-stats">
				<view class="stat-card">
					<view class="stat-value">{{ score }}</view>
					<view class="stat-label">总得分</view>
				</view>
				<view class="stat-card">
					<view class="stat-value">{{ correctCount }}/10</view>
					<view class="stat-label">正确题数</view>
				</view>
				<view class="stat-card">
					<view class="stat-value">{{ Math.round((correctCount / 10) * 100) }}%</view>
					<view class="stat-label">正确率</view>
				</view>
			</view>
			
			<view class="result-actions">
				<view class="action-btn secondary" @click="goBack">
					<u-icon name="arrow-left" size="16" color="#666"></u-icon>
					<text>返回</text>
				</view>
				<view class="action-btn primary" @click="restartTraining">
					<u-icon name="reload" size="16" color="#fff"></u-icon>
					<text>重新开始</text>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
import questionGenerator from './questionGenerator';
import publicModule from '@/components/public-module';

export default {
	components: {
		publicModule
	},
	data() {
		return {
			title: '找数训练',
			trainingStarted: false,
			showResult: false,
			currentMaterial: null,
			currentQuestionIndex: 0,
			selectedOption: null,
			showFeedback: false,
			isCorrect: false,
			remainingTime: 0,
			timer: null,
			questions: [],
			score: 0,
			correctCount: 0
		};
	},

	onLoad(options) {
		// 直接开始训练
		this.$nextTick(() => {
			this.startTraining();
		});
	},

	computed: {
		currentQuestion() {
			return this.questions[this.currentQuestionIndex] || {};
		},
		correctAnswerText() {
			if (!this.currentQuestion || !this.currentQuestion.options) return '';
			const correctOption = this.currentQuestion.options.find(opt => opt.isCorrect);
			return correctOption ? `${correctOption.label}: ${correctOption.text}` : '';
		},
		// 保持原始选项顺序，不再按ABCD重新排序
		sortedOptions() {
			if (!this.currentQuestion || !this.currentQuestion.options) return [];
			// 直接返回原始选项，保持生成器中的随机分布
			return this.currentQuestion.options;
		}
	},

	methods: {
		startTraining() {
			// 生成训练材料
			const material = questionGenerator.generateTrainingMaterial();
			this.currentMaterial = material;
			this.questions = material.questions;
			
			// 设置时间从0开始
			this.remainingTime = 0;
			this.startTimer();
			
			this.trainingStarted = true;
			this.currentQuestionIndex = 0;
			this.selectedOption = null;
			this.showFeedback = false;
			this.score = 0;
		},

		startTimer() {
			this.timer = setInterval(() => {
				this.remainingTime++; // 改为正计时
			}, 1000);
		},

		formatTime(seconds) {
			const minutes = Math.floor(seconds / 60);
			const remainingSeconds = seconds % 60;
			return `${minutes}:${remainingSeconds.toString().padStart(2, '0')}`;
		},

		selectOption(selectedOption) {
			if (this.showFeedback) return;
			
			this.selectedOption = selectedOption;
			this.isCorrect = selectedOption.isCorrect;
			this.showFeedback = true;

			if (this.isCorrect) {
				this.score += 10;
				this.correctCount++;
				uni.showToast({
					title: '回答正确！',
					icon: 'success'
				});
			} else {
				uni.showToast({
					title: '回答错误！',
					icon: 'none'
				});
			}
		},

		goToNextQuestion() {
			this.showFeedback = false;
			this.selectedOption = null;
			
			if (this.currentQuestionIndex < 9) { // 改为9，因为索引从0开始，总兡10题
				this.currentQuestionIndex++;
			} else {
				this.endTraining();
			}
		},

		endTraining() {
			clearInterval(this.timer);
			this.trainingStarted = false;
			this.showResult = true;
		},

		// 重新开始训练
		restartTraining() {
			this.showResult = false;
			this.resetTrainingData();
			this.startTraining();
		},

		// 返回上一页
		goBack() {
			uni.navigateBack();
		},

		// 重置训练数据
		resetTrainingData() {
			this.currentQuestionIndex = 0;
			this.selectedOption = null;
			this.showFeedback = false;
			this.score = 0;
			this.correctCount = 0;
			this.remainingTime = 0;
			if (this.timer) {
				clearInterval(this.timer);
				this.timer = null;
			}
		}
	},

	beforeDestroy() {
		if (this.timer) {
			clearInterval(this.timer);
		}
	}
};
</script>

<style lang="scss" scoped>
.view-contain {
	min-height: 100vh;
	background-color: #f5f6fa;
	display: flex;
	flex-direction: column;
	position: relative;
	
	// 试卷头部样式
	.paper-header {
		background: #fff;
		padding: 20rpx 30rpx;
		margin: 0;
		box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
		border-bottom: 1rpx solid #eee;
		position: sticky;
		top: 88rpx; // 导航栏高度
		z-index: 100;
		
		.paper-title {
			font-size: 32rpx;
			font-weight: bold;
			color: #333;
			text-align: center;
			margin-bottom: 16rpx;
		}
		
		.paper-info {
			display: flex;
			justify-content: space-between;
			align-items: center;
			gap: 20rpx;
			
			.info-item {
				display: flex;
				align-items: center;
				flex: 1;
				justify-content: center;
				
				.info-label {
					font-size: 24rpx;
					color: #666;
					margin-right: 8rpx;
				}
				
				.info-value {
					font-size: 24rpx;
					color: #4b80af;
					font-weight: bold;
				}
			}
		}
	}
	
	// 材料部分样式
	.material-section {
		margin: 20rpx 30rpx;
		border-radius: 12rpx;
		box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
		overflow: hidden;
		flex: 1;
		display: flex;
		flex-direction: column;
		height: calc(100vh - 400rpx); // 减去导航栏、头部和底部选项区域高度
		
		
		.material-content {
			flex: 1;
			line-height: 1.8;
			font-size: 28rpx;
			overflow-y: auto;
			overflow-x: hidden;
			word-wrap: break-word;
			word-break: break-all;
			
			:deep(rich-text) {
				text-indent: 2em;
				max-width: 100%;
				overflow-wrap: break-word;
				p {
					margin-bottom: 1em;
					max-width: 100%;
					word-wrap: break-word;
				}
			}
		}
	}
	
	// 结果页面样式
	.result-area {
		padding: 40rpx 20rpx;
		min-height: calc(100vh - 88rpx); // 减去导航栏高度
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		
		.result-header {
			text-align: center;
			margin-bottom: 50rpx;
			
			.result-title {
				font-size: 42rpx;
				font-weight: bold;
				color: #333;
				margin-bottom: 16rpx;
			}
			
			.result-subtitle {
				font-size: 28rpx;
				color: #666;
				line-height: 1.5;
			}
		}
		
		.result-stats {
			display: flex;
			justify-content: space-around;
			width: 100%;
			margin-bottom: 60rpx;
			
			.stat-card {
				background: #fff;
				padding: 40rpx 20rpx;
				border-radius: 16rpx;
				box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
				text-align: center;
				flex: 1;
				margin: 0 10rpx;
				
				.stat-value {
					font-size: 36rpx;
					font-weight: bold;
					color: #4b80af;
					margin-bottom: 8rpx;
					display: block;
				}
				
				.stat-label {
					font-size: 24rpx;
					color: #666;
					display: block;
				}
			}
		}
		
		.result-actions {
			display: flex;
			gap: 20rpx;
			width: 100%;
			justify-content: center;
			
			.action-btn {
				display: flex;
				align-items: center;
				justify-content: center;
				gap: 8rpx;
				padding: 24rpx 40rpx;
				border-radius: 30rpx;
				font-size: 30rpx;
				font-weight: bold;
				transition: all 0.3s ease;
				min-width: 200rpx;
				
				&.primary {
					background: #4b80af;
					color: #fff;
				}
				
				&.secondary {
					background: #f8f9fa;
					color: #666;
					border: 2rpx solid #ddd;
				}
			}
		}
	}
		

		// 训练区域样式
		.training-area {
			flex: 1;
			display: flex;
			flex-direction: column;
			padding-top: 0;
		}
		

	// 问题和选项区域样式
	.options-section {
		position: fixed;
		bottom: 0;
		left: 30rpx;
		right: 30rpx;
		background: #fff;
		padding: 20rpx;
		box-shadow: 0 -2rpx 12rpx rgba(0, 0, 0, 0.1);
		z-index: 999;
		max-height: 60vh;
		overflow-y: auto;
		border-radius: 12rpx 12rpx 0 0;
		
		.question-content {
			padding-bottom: 20rpx;
			border-bottom: 1rpx solid #eee;
			margin-bottom: 20rpx;
			
			.question-title {
				display: flex;
				align-items: flex-start;
				gap: 8rpx;
				line-height: 1.6;
				
				.question-number {
					font-size: 30rpx;
					font-weight: bold;
					color: #333;
					flex-shrink: 0;
				}
				
				.question-text {
					font-size: 30rpx;
					color: #333;
					flex: 1;
					word-wrap: break-word;
					word-break: break-all;
				}
			}
		}
		
		.options {
			display: flex;
			flex-direction: column;
			gap: 16rpx;
			
			.option {
				display: flex;
				align-items: flex-start;
				padding: 20rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 8rpx;
				transition: all 0.3s ease;
				cursor: pointer;
				background: #fafafa;
				
				&:hover {
					border-color: #d9d9d9;
					background: #f5f5f5;
				}
				
				&.selected {
					border-color: #4b80af;
					background: #e6f7ff;
					box-shadow: 0 0 0 2rpx rgba(75, 128, 175, 0.2);
				}
				
				&.correct {
					border-color: #52c41a;
					background: #f6ffed;
					box-shadow: 0 0 0 2rpx rgba(82, 196, 26, 0.2);
				}
				
				&.wrong {
					border-color: #f5222d;
					background: #fff1f0;
					box-shadow: 0 0 0 2rpx rgba(245, 34, 45, 0.2);
				}
				
				.option-label {
					font-size: 28rpx;
					color: #333;
					font-weight: bold;
					margin-right: 16rpx;
					flex-shrink: 0;
					min-width: 32rpx;
				}
				
				.option-text {
					flex: 1;
					font-size: 28rpx;
					color: #333;
					line-height: 1.5;
					word-wrap: break-word;
					word-break: break-all;
				}
			}
		}
	}
	
	// 答案反馈区域样式
	.feedback-area {
		position: fixed;
		bottom: 0;
		left: 30rpx;
		right: 30rpx;
		padding: 30rpx;
		border-radius: 12rpx 12rpx 0 0;
		text-align: center;
		background: #fff;
		box-shadow: 0 -2rpx 12rpx rgba(0, 0, 0, 0.1);
		z-index: 1000;
		
		.feedback-text {
			font-size: 32rpx;
			font-weight: bold;
			margin-bottom: 16rpx;
			padding: 12rpx 24rpx;
			border-radius: 8rpx;
			
			&.correct {
				color: #52c41a;
				background: #f6ffed;
				border: 1rpx solid #b7eb8f;
			}
			
			&.wrong {
				color: #f5222d;
				background: #fff1f0;
				border: 1rpx solid #ffa39e;
			}
		}
		
		.correct-answer {
			font-size: 28rpx;
			color: #666;
			margin-bottom: 20rpx;
			padding: 12rpx;
			background: #f8f9fa;
			border-radius: 6rpx;
			word-wrap: break-word;
			word-break: break-all;
		}
		
		.next-btn {
			display: inline-block;
			padding: 16rpx 32rpx;
			background: #4b80af;
			color: #fff;
			border-radius: 25rpx;
			font-size: 28rpx;
			font-weight: bold;
			transition: all 0.3s ease;
			cursor: pointer;
			
			&:active {
				opacity: 0.8;
				transform: scale(0.98);
			}
		}
	}
}
</style>